const fs = require('fs')
const path = require('path')

function readFilePromise(fileName) {
    let promise = new Promise((resolve,reject)=>{
        fs.readFile(path.join(__dirname,fileName),'utf-8',(err,date)=>{
            if(err){ reject(err) }
        else{
            resolve(date)
        }
        })
    })
    return promise
}
function writeFilePromise(fileName,content) {
    return new Promise((resolve,reject)=>{
        fs.writeFile(path.join(__dirname,fileName),content,(error)=>{
            if(error){ reject(error) }
            else{
                resolve()
            }
        })
    })
}
let content = '';

async function main() {
    try{
        await Promise.all([writeFilePromise('1.txt','这是第一个文件夹'),writeFilePromise('2.txt','这是第二个文件夹')])
        const [text1,text2] = await Promise.all([readFilePromise('1.txt'),readFilePromise('2.txt')])
        content += text1 + '\n' + text2;
        console.log(content);
        await writeFilePromise('3.txt',content)
    }catch(error){
        reject(error)
    }finally{
        console.log('success');
    }
} 
main();

//promise的三种状态和使用方式 resolve，reject，pending（等待状态）
